
package com.xiaoma.myaudience.biz.model;

import com.xiaoma.myaudience.util.UtilConstants;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;

public class PlayRecordLoginInfo extends PlayRecordNoLoginInfo {
    public static final String USERID = "userid";

    /**
     * 表明这条记录是否已同步服务器.<br/>
     * 0:未同步 {@link #NO_SYNCHRONIZED}<br/>
     * 1:已同步 {@link #SYNCHRONIZED}<br/>
     */
    public static final String STATUS = "status";

    public static final int NO_SYNCHRONIZED = 0;

    public static final int SYNCHRONIZED = 1;

    public String mUserId;

    public int mStatus;

    public String generateParamStr() {
        return new StringBuilder().append(mPid).append(UtilConstants.PARAM_SEPARATOR1)
                 .append(mVid).append(UtilConstants.PARAM_SEPARATOR1)
                 .append(mEndTag).append(UtilConstants.PARAM_SEPARATOR1)
                 .append(mTimeSpan).append(UtilConstants.PARAM_SEPARATOR1)
                 .append(mWatchTime).append(UtilConstants.PARAM_SEPARATOR1)
                 .append(mVnumber).toString();
    }

    @Override
    public ContentValues generateContentValues() {
        ContentValues values = super.generateContentValues();
        values.put(USERID, mUserId);
        values.put(STATUS, mStatus);
        return values;
    }

    @Override
    public void readFromCursor(Cursor cursor) {
        super.readFromCursor(cursor);
        int index = -1;
        if ((index = cursor.getColumnIndex(USERID)) != -1) {
            mUserId = cursor.getString(index);
            index = -1;
        }
        if ((index = cursor.getColumnIndex(STATUS)) != -1) {
            mStatus = cursor.getInt(index);
            index = -1;
        }
    }

    @Override
    public void readFromJSONObject(JSONObject o) throws JSONException {
        super.readFromJSONObject(o);
        if (o != null) {
            if (o.has(USERID))
                mUserId = o.getString(USERID);
        }
    }

    @Override
    public void readFromParcel(Parcel source) {
        super.readFromParcel(source);
        mUserId = source.readString();
        mStatus = source.readInt();
    }

    @Override
    public void writeToParcel(Parcel parcel, int paramInt) {
        super.writeToParcel(parcel, paramInt);
        parcel.writeString(mUserId);
        parcel.writeInt(mStatus);
    }

    public static final Parcelable.Creator<PlayRecordLoginInfo> CREATOR = new Creator<PlayRecordLoginInfo>() {
        public PlayRecordLoginInfo createFromParcel(Parcel source) {
            PlayRecordLoginInfo info = new PlayRecordLoginInfo();
            info.readFromParcel(source);
            return info;
        }

        public PlayRecordLoginInfo[] newArray(int size) {
            return new PlayRecordLoginInfo[size];
        }
    };

}
